package com.ssamkj.wstar.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import com.ssamkj.rssbot.framework.IRSSListService;
import com.ssamkj.rssbot.parser.dto.RSS_RawInfo;
import com.ssamkj.wstar.dto.godMode.LocaleDTO;
import com.ssamkj.wstar.dto.godMode.RSS_Attr1;
import com.ssamkj.wstar.dto.godMode.RSS_Attr2;
import com.ssamkj.wstar.dto.godMode.RSS_List;
import com.ssamkj.wstar.service.IGodModeService;
import com.ssamkj.wstar.service.dao.IGodModeMapper;

public class GodModeServiceImpl implements IGodModeService, IRSSListService{
	private static String RESOURCE = "c:\\work\\W-StarNews\\workspace\\W-Stars\\src\\main\\resources\\database\\xml";
	
	IGodModeMapper mapper;
	
	public void setMapper(IGodModeMapper mapper) {
		this.mapper = mapper;
	}
	
	enum NATION_LIST {
		MYID("myid"), NAME("name"), ACTIVE("active"), DT("dt");

		private String name;

		NATION_LIST(String name) {
			this.name = name;
		}

		public String getName() {
			return this.name;
		}
	}
	
	enum RSS_LIST{
		MYID("myid"), NAME("name"), URL("url"), DT("dt"), ATTR1("attr1"), ATTR2("attr2"), NATION("nation");

		private String name;

		RSS_LIST(String name) {
			this.name = name;
		}

		public String getName() {
			return this.name;
		}
	}

	enum ATTR1{
		MYID("myid"), NAME("name"), ACTIVE("active");
		

		private String name;

		ATTR1(String name) {
			this.name = name;
		}

		public String getName() {
			return this.name;
		}
	}
	

	enum ATTR2{
		MYID("myid"), ATTR1_ID("attr1_id"), NAME("name"), ACTIVE("active");
		

		private String name;

		ATTR2(String name) {
			this.name = name;
		}

		public String getName() {
			return this.name;
		}
	}
	
	@Override
	public List<RSS_List> readRSS_List(){
		List<RSS_List> rsslist_dto = new ArrayList<RSS_List>(); 
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\RSS_LIST.xml");
			Document doc = builder.parse(f);


			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("item");

			int length = list.getLength();
			for (int i = 0; i < length; i++) {
				RSS_List dto = new RSS_List();
				Node node = list.item(i);
				System.out.println(node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element headLineElement = (Element) node;

					for (RSS_LIST rss_list : RSS_LIST.values()) {
						String tagName = rss_list.getName().toLowerCase();
						NodeList nameElement = headLineElement
								.getElementsByTagName(tagName);
						Element subItem = (Element) nameElement.item(0);
						NodeList subElement1 = subItem.getChildNodes();
						System.out.println("key = " + tagName + "\tvalue = "
								+ subElement1.item(0).getNodeValue());
						
						switch(rss_list){
						case MYID:{
							dto.setMyid(Integer.parseInt(subElement1.item(0).getNodeValue()));
							break;
						}
						case ATTR1 :{
							dto.setAttr1(Integer.parseInt(subElement1.item(0).getNodeValue()));
							break;
						}
						case ATTR2 :{
							dto.setAttr2(Integer.parseInt(subElement1.item(0).getNodeValue()));
							break;
						}
						case NAME :{
							dto.setName(subElement1.item(0).getNodeValue());
							break;
						}
						case DT :{
							dto.setDt(Integer.parseInt(subElement1.item(0).getNodeValue()));
							break;
						}
						case NATION :{
							dto.setNation(subElement1.item(0).getNodeValue());
							break;
						}
						case URL :{
							dto.setUrl(subElement1.item(0).getNodeValue());
							break;
						}
						}
						
					}
					rsslist_dto.add(dto);
				}
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		return rsslist_dto;
	}

	@Override
	public List<RSS_Attr2> readAttr2List(){
		List<RSS_Attr2> attr2_dto = new ArrayList<RSS_Attr2>();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\RSS_ATTR2.xml");
			Document doc = builder.parse(f);


			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("item");

			int length = list.getLength();
			for (int i = 0; i < length; i++) {
				RSS_Attr2 dto = new RSS_Attr2();
				Node node = list.item(i);
				System.out.println(node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element headLineElement = (Element) node;

					for (ATTR2 attr2 : ATTR2.values()) {
						String tagName = attr2.getName().toLowerCase();
						NodeList nameElement = headLineElement
								.getElementsByTagName(tagName);
						Element subItem = (Element) nameElement.item(0);
						NodeList subElement1 = subItem.getChildNodes();
						System.out.println("key = " + tagName + "\tvalue = "
								+ subElement1.item(0).getNodeValue());
						
						switch(attr2){
						case MYID:{
							dto.setMyid(Integer.parseInt(subElement1.item(0).getNodeValue()));
							break;
						}
						case ATTR1_ID :{
							dto.setAttr1_id(Integer.parseInt(subElement1.item(0).getNodeValue()));
							break;
						}
						case NAME :{
							dto.setName(subElement1.item(0).getNodeValue());
							break;
						}
						case ACTIVE :{
							dto.setActive(subElement1.item(0).getNodeValue().equals("true"));
							break;
						}
						}
						
					}
					attr2_dto.add(dto);
				}
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		return attr2_dto;
	}
	
	@Override
	public List<RSS_Attr1> readAttr1List(){
		List<RSS_Attr1> attr1_dto = new ArrayList<RSS_Attr1> ();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\RSS_ATTR1.xml");
			Document doc = builder.parse(f);


			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("item");

			int length = list.getLength();
			for (int i = 0; i < length; i++) {
				RSS_Attr1 dto = new RSS_Attr1();
				Node node = list.item(i);
				System.out.println(node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element headLineElement = (Element) node;

					for (ATTR1 attr1 : ATTR1.values()) {
						String tagName = attr1.getName().toLowerCase();
						NodeList nameElement = headLineElement
								.getElementsByTagName(tagName);
						Element subItem = (Element) nameElement.item(0);
						NodeList subElement1 = subItem.getChildNodes();
						System.out.println("key = " + tagName + "\tvalue = "
								+ subElement1.item(0).getNodeValue());
						
						switch(attr1){
						case MYID:{
							dto.setMyid(Integer.parseInt(subElement1.item(0).getNodeValue()));
							break;
						}
						case NAME :{
							dto.setName(subElement1.item(0).getNodeValue());
							break;
						}
						case ACTIVE :{
							dto.setActive(subElement1.item(0).getNodeValue().equals("true"));
							break;
						}
						}
						
					}
					attr1_dto.add(dto);
				}
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		return attr1_dto;
	}
	
	@Override
	public List<LocaleDTO> readNationList(){
		List<LocaleDTO> locale = new ArrayList<LocaleDTO> ();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\NATION_LIST.xml");
			Document doc = builder.parse(f);


			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("item");

			int length = list.getLength();
			for (int i = 0; i < length; i++) {
				LocaleDTO dto = new LocaleDTO();
				Node node = list.item(i);
				System.out.println(node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element headLineElement = (Element) node;

					for (NATION_LIST nation : NATION_LIST.values()) {
						String tagName = nation.getName().toLowerCase();
						NodeList nameElement = headLineElement
								.getElementsByTagName(tagName);
						Element subItem = (Element) nameElement.item(0);
						NodeList subElement1 = subItem.getChildNodes();
						System.out.println("key = " + tagName + "\tvalue = "
								+ subElement1.item(0).getNodeValue());
						
						switch(nation){
						case MYID:{
							dto.setLoc(subElement1.item(0).getNodeValue());
							break;
						}
						case NAME :{
							dto.setName(subElement1.item(0).getNodeValue());
							break;
						}
						case ACTIVE :{
							dto.setActive(subElement1.item(0).getNodeValue().equals("true"));
							break;
						}
						
						case DT :{
							dto.setDt(Integer.parseInt(subElement1.item(0).getNodeValue().toString()));
							break;
						}
						}
						
					}
					locale.add(dto);
				}
			}

		} catch (ParserConfigurationException e) {
			
			e.printStackTrace();
		} catch (SAXException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} 
		
		return locale;
	}
	
	@Override
	public void insertAttr1(RSS_Attr1 dto){
		try {
			System.out.println(dto);
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\RSS_ATTR1.xml");
			Document doc = builder.parse(f);

			Text val1 = doc.createTextNode(String.valueOf(dto.getMyid()));
			Node myid = doc.createElement("myid");
			myid.appendChild(val1);

			Text val2 = doc.createTextNode(dto.getName());
			Node name = doc.createElement("name");
			name.appendChild(val2);

			Text val3 = doc.createTextNode("true");
			Node active = doc.createElement("active");
			active.appendChild(val3);

			
			Node item = doc.createElement("item");
			item.appendChild(myid);
			item.appendChild(name);
			item.appendChild(active);
			
			doc.getDocumentElement().appendChild(item);

			doc.getDocumentElement().normalize();
			saveFile(doc, f);

			
		} catch (ParserConfigurationException e) {
			
			e.printStackTrace();
		} catch (SAXException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} 
		catch (TransformerException e) {
			
			e.printStackTrace();
		}
	}
	
	@Override
	public void insertAttr2(RSS_Attr2 dto){
		try {
			System.out.println(dto);
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\RSS_ATTR2.xml");
			Document doc = builder.parse(f);

			Text val1 = doc.createTextNode(String.valueOf(dto.getMyid()));
			Node myid = doc.createElement("myid");
			myid.appendChild(val1);

			Text val2 = doc.createTextNode(dto.getName());
			Node name = doc.createElement("name");
			name.appendChild(val2);

			Text val3 = doc.createTextNode("true");
			Node active = doc.createElement("active");
			active.appendChild(val3);

			Text val4 = doc.createTextNode(String.valueOf(dto.getAttr1_id()));
			Node attr1_id = doc.createElement("attr1_id");
			attr1_id.appendChild(val4);

			
			Node item = doc.createElement("item");
			item.appendChild(myid);
			item.appendChild(name);
			item.appendChild(active);
			item.appendChild(attr1_id);
			
			doc.getDocumentElement().appendChild(item);

			doc.getDocumentElement().normalize();
			saveFile(doc, f);

			
		} catch (ParserConfigurationException e) {
			
			e.printStackTrace();
		} catch (SAXException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} 
		catch (TransformerException e) {
			
			e.printStackTrace();
		}
	}
	

	@Override
	public void insertRSS(RSS_List dto){
		try {
			System.out.println(dto);
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\RSS_LIST.xml");
			Document doc = builder.parse(f);
			
			Text val1 = doc.createTextNode(String.valueOf(dto.getMyid()));
			Node myid = doc.createElement("myid");
			myid.appendChild(val1);

			Text val2 = doc.createTextNode(dto.getName());
			Node name = doc.createElement("name");
			name.appendChild(val2);

			Text val3 = doc.createTextNode(dto.getUrl());
			Node url = doc.createElement("url");
			url.appendChild(val3);


			Text val4 = doc.createTextNode(String.valueOf((new Date().getTime()/1000)));
			Node dt = doc.createElement("dt");
			dt.appendChild(val4);

			
			Text val5 = doc.createTextNode(String.valueOf(dto.getAttr1()));
			Node attr1 = doc.createElement("attr1");
			attr1.appendChild(val5);
			
			Text val6 = doc.createTextNode(String.valueOf(dto.getAttr2()));
			Node attr2 = doc.createElement("attr2");
			attr2.appendChild(val6);
			
			Text val7 = doc.createTextNode(dto.getNation());
			Node nation = doc.createElement("nation");
			nation.appendChild(val7);
			
			Node item = doc.createElement("item");
			item.appendChild(myid);
			item.appendChild(name);
			item.appendChild(url);
			item.appendChild(dt);
			item.appendChild(attr1);
			item.appendChild(attr2);
			item.appendChild(nation);
			
			doc.getDocumentElement().appendChild(item);

			doc.getDocumentElement().normalize();
			saveFile(doc, f);

			
		} catch (ParserConfigurationException e) {
			
			e.printStackTrace();
		} catch (SAXException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} 
		catch (TransformerException e) {
			
			e.printStackTrace();
		}
	}
	
	@Override
	public void insertNation(LocaleDTO dto){
		try {
			System.out.println(dto);
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\NATION_LIST.xml");
			Document doc = builder.parse(f);

			Text val1 = doc.createTextNode(dto.getLoc());
			Node myid = doc.createElement("myid");
			myid.appendChild(val1);

			Text val2 = doc.createTextNode(dto.getName());
			Node name = doc.createElement("name");
			name.appendChild(val2);

			Text val3 = doc.createTextNode("true");
			Node active = doc.createElement("active");
			active.appendChild(val3);

			Text val4 = doc.createTextNode(String.valueOf((new Date().getTime()/1000)));
			Node dt = doc.createElement("dt");
			dt.appendChild(val4);
			
			Node item = doc.createElement("item");
			item.appendChild(myid);
			item.appendChild(name);
			item.appendChild(active);
			item.appendChild(dt);
			
			doc.getDocumentElement().appendChild(item);

			doc.getDocumentElement().normalize();
			saveFile(doc, f);

			
		} catch (ParserConfigurationException e) {
			
			e.printStackTrace();
		} catch (SAXException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} 
		catch (TransformerException e) {
			
			e.printStackTrace();
		}
	}

	
	protected boolean saveFile(Document doc, File f) throws TransformerException {
		// 변환기 공장 생성
		TransformerFactory factory = TransformerFactory.newInstance();
		// 변환기 생성
		javax.xml.transform.Transformer transformer = factory.newTransformer();
		// XML 선언과 문서 유형 선언 내용 설정
		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		// DOMSource 객체 생성
		DOMSource source = new DOMSource(doc);
		// StreamResult 객체 생성
		StreamResult result = new StreamResult(f);
		// 파일로 저장하기
		transformer.transform(source, result);
		System.out.println("저장 완료");
		
		
		return false;
	}

	@Override
	public List<RSS_RawInfo> getRssList() {
		// TODO Auto-generated method stub
		List<RSS_RawInfo> ret = new ArrayList<RSS_RawInfo>();
		List<RSS_List> list =  readRSS_List();
		RSS_RawInfo raw;
		for(RSS_List l : list){
			raw = new RSS_RawInfo();
			
			raw.setMyid(String.valueOf(l.getMyid()));
			raw.setName(l.getName());
			raw.setNation(l.getNation());
			raw.setAttr1(l.getAttr1());
			raw.setAttr2(l.getAttr2());
			raw.setDt(l.getDt());
			raw.setUrl(l.getUrl());
			
			ret.add(raw);
		}
		
		
		
		return ret;
	}
}
